381B - Sereja and Stairs - CodeForces Solution


greedy implementation sortings *1100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define fst ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long 
#define sz(x) int(x.size())
#define ft first
#define sd second
#define endl '\n'
#define pb push_back
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
void solve()
{  
    int v[12312];
    int n; cin >> n;    
    for(int i = 0; i < n; i++){
        int x; cin >> x;
        v[x]++;
    }
 
    vector<int> res;
    int maxi = 0;
    for(int i = 1; i <= 5000; i++)
        if(v[i] >= 1){
            maxi = i;
            v[i]--;
            res.push_back(i);
        }
    for(int i = maxi-1; i >= 1; i--)
        if(v[i] >= 1){
            v[i]--;
            res.push_back(i);
        }
    cout << res.size() << endl;
    for(int val : res)
        cout << val << " ";
    cout << endl;
}
int32_t main()
{
    fst
    int ttt=1;
    //cin >> ttt;
    while(ttt--)
    {
        solve();
    }
    return 0;
}








Comments

Submit
0 Comments
More Questions

1459B - Move and Turn
1006F - Xor-Paths
706C - Hard problem
304C - Lucky Permutation Triple
1301C - Ayoub's function
38E - Let's Go Rolling
171G - Mysterious numbers - 2
1183C - Computer Game
400C - Inna and Huge Candy Matrix
417A - Elimination
222A - Shooshuns and Sequence
1736A - Make A Equal to B
1736B - Playing with GCD
887C - Solution for Cube
1737C - Ela and Crickets
1741C - Minimize the Thickness
1741A - Compare T-Shirt Sizes
1741D - Masha and a Beautiful Tree
109B - Lucky Probability
1741B - Funny Permutation
1741E - Sending a Sequence Over the Network
344B - Simple Molecules
370A - Rook Bishop and King
546E - Soldier and Traveling
1741G - Kirill and Company
1200B - Block Adventure
1088B - Ehab and subtraction
1270B - Interesting Subarray
478C - Table Decorations
1304C - Air Conditioner